package dev.blogmoon.cn.admin.controller;

import dev.blogmoon.cn.common.entity.Result;
import dev.blogmoon.cn.system.enums.MenuSourceEnum;
import dev.blogmoon.cn.system.module.MenuTreeDTO;
import dev.blogmoon.cn.system.repository.SysMenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/admin/menu")
public class MenuController {

    @Autowired
    private SysMenuRepository sysMenuRepository;

    @GetMapping("/routes")
    public Result getMenuList() {

        return Result.success(sysMenuRepository.getMenuTree(MenuSourceEnum.SYS));
    }


    @GetMapping("/tree")
    public Result getAllMenus(){
        return Result.success(sysMenuRepository.getMenuTree(MenuSourceEnum.SYS));
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id") Long id) {
        return Result.success(sysMenuRepository.detail(id));
    }


    @PostMapping("/save")
    public Result save(@RequestBody MenuTreeDTO menuTreeDTO){
        sysMenuRepository.save(menuTreeDTO,MenuSourceEnum.SYS);
        return Result.success("保存成功");
    }

    @PostMapping("/remove/{id}")
    public Result remove(@PathVariable("id") Long id){
        sysMenuRepository.removeById(id);
        return Result.success("删除成功");
    }
}
